package com.lb.jwitter.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.TblCTDTMonHocTienQuyetService;
import com.lb.jwitter.service.TblCTDTThayTheMonHocService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TblCTDTThayTheMonHocSaveFromXLSAction {
	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}
	
	//--------------------------------------------
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}

	private String destFile;

	public String getDestFile() {
		return destFile;
	}

	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	
	public String execute() throws Exception {

		try {

			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";

			ApplicationContext context = ApplicationContextProvider.getApplicationContext();
			// get json from excel
			jsons = ExcelToJSon.execute(destFile);
			
			
			TblCTDTThayTheMonHocService tblCTDTThayTheMonHocService = (TblCTDTThayTheMonHocService) context.getBean("tbl_CTDT_ThayTheMonHocClient");
			tblCTDTThayTheMonHocService.createListtblCTDTThayTheMonHocByJSON(jsons.toString());
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi add mon ho from excel Action");
			e.printStackTrace();
			return Action.ERROR;
		}

	}
}
